//
//  MineRechargeViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/9/4.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "MineRechargeViewController.h"
#import "MineAnotherRechargeViewController.h"

#import "MineRechargeCompleteViewController.h"//测试
@interface MineRechargeViewController ()

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIButton *aBtn;
@property (nonatomic, copy)  NSString *num;
@property (nonatomic, copy) NSString *secretKey;

@property (nonatomic, copy) NSDictionary *param;//用于传值给微信支付
@end


@implementation MineRechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackItem];
    self.title = @"账户余额充值";
    [self initGWView];
    
}


- (void)starNetwork
{
    //支付宝
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/order/AlipayChargePailebao?num=%@&pay_type=%@", GW_Domain, _num, @"2"] parameters:@{@"num":_num} completionBlockWithSuccess:^(NSDictionary *resultDict) {
        NSLog(@"支付宝 %@", resultDict);
        NSDictionary *dic = resultDict[@"data"];
        _secretKey = dic[@"alipayStr"];
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", errorMsg);
    }];
    
    //微信
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/order/AlipayChargePailebao?num=%@&pay_type=%@", GW_Domain, _num, @"1"] parameters:@{@"num":_num, @"pay_type":@"1"} completionBlockWithSuccess:^(NSDictionary *resultDict) {
        NSLog(@"微信 %@", resultDict);
        _param = resultDict[@"data"];
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", errorMsg);
    }];
}


- (void)initGWView
{
    _button = [UIButton  buttonWithType:UIButtonTypeCustom];
    [_button setTitle:@"1000" forState:0];
    [_button setTitleColor:ColorFrom16(0x202020) forState:0];
    _button.titleLabel.font = PFRFONT(20);
    _button.layer.cornerRadius = 4;
    _button.layer.masksToBounds = YES;
    _button.layer.backgroundColor = ColorWhite.CGColor;
    _button.layer.borderColor = ColorFrom16(0xFEDE6D).CGColor;
    _button.layer.borderWidth = 1;
    [self.view addSubview:_button];
    [_button addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(58);
        make.left.equalTo(self.view).offset(40);
        if(IS_IPHONE5)
            make.width.equalTo(@110);
        else
            make.width.equalTo(@140);
        make.height.equalTo(@98);
    }];
    
    _aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_aBtn setTitle:@"2000" forState:0];
    [_aBtn setTitleColor:ColorFrom16(0x202020) forState:0];
    _aBtn.titleLabel.font = PFRFONT(20);
    _aBtn.layer.cornerRadius = 4;
    _aBtn.layer.masksToBounds = YES;
    _aBtn.layer.backgroundColor = ColorWhite.CGColor;
    _aBtn.layer.borderColor = ColorFrom16(0xFEDE6D).CGColor;
    _aBtn.layer.borderWidth = 1;
    [_aBtn addTarget:self action:@selector(handleAbtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_aBtn];
    [_aBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_button);
        make.right.equalTo(self.view).offset(-40);
        if(IS_IPHONE5)
            make.width.equalTo(@110);
        else
            make.width.equalTo(@140);
        make.height.equalTo(@98);
    }];
    
    
    UIButton *rechargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rechargeBtn setTitle:@"选择其他充值金额" forState:0];
    [rechargeBtn setTitleColor:ColorFrom16(0x7F7E7D) forState:0];
    rechargeBtn.titleLabel.font = PFRFONT(14);
    [rechargeBtn addTarget:self action:@selector(handleRechargeBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rechargeBtn];
    [rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_button.mas_bottom).offset(40);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.greaterThanOrEqualTo(@118);
        make.height.equalTo(@20);
    }];
    
    UIButton *gotoRecharge = [UIButton buttonWithType:UIButtonTypeCustom];
    [gotoRecharge setTitle:@"充值" forState:0];
    [gotoRecharge setTitleColor:ColorFrom16(0x202020) forState:0];
    gotoRecharge.titleLabel.font = PFRFONT(16);
    gotoRecharge.layer.backgroundColor = ColorFrom16(0xFEDE6D).CGColor;
    gotoRecharge.layer.cornerRadius = 7;
    [gotoRecharge addTarget:self action:@selector(gotoRecharge) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:gotoRecharge];
    [gotoRecharge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(rechargeBtn.mas_bottom).offset(20);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@44);
    }];
    
    _bottomView = [UIView new];
    _bottomView.backgroundColor = ColorWhite;
    _bottomView.hidden = YES;
    [self.view addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.left.and.right.equalTo(self.view);
        make.height.equalTo(@245);
    }];
    
    UILabel *label = [UILabel new];
    label.text = @"支付方式";
    label.textColor = ColorFrom16(0x333333);
    label.backgroundColor = ColorFrom16(0xf0f0f0);
    label.font = PFRFONT(14);
    label.textAlignment = 1;
    [_bottomView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bottomView);
        make.left.and.right.equalTo(_bottomView);
        make.height.equalTo(@50);
    }];
    
    //关闭按钮
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:IMAGE(@"close") forState:0];
    [closeBtn setEnlargeEdge:20];
    [closeBtn addTarget:self action:@selector(handleClose) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bottomView).offset(20);
        make.centerY.equalTo(label.mas_centerY);
        make.width.equalTo(@11);
        make.height.equalTo(@11);
    }];
    [label bringSubviewToFront:closeBtn];
    
    UIButton *wxBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    [wxBtn setTitle:@" 微信支付" forState:0];
    [wxBtn setImage:IMAGE(@"weixin") forState:0];
    wxBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    wxBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
    [wxBtn addTarget:self action:@selector(handleWxBtn) forControlEvents:UIControlEventTouchUpInside];
    [wxBtn setTitleColor:ColorFrom16(0x333333) forState:0];
    wxBtn.titleLabel.font = PFRFONT(14);
    [_bottomView addSubview:wxBtn];
    [wxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(34);
        make.left.and.right.equalTo(self.view);
        make.height.equalTo(@44);
    }];
    
    UIButton *payBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    [payBtn setTitle:@"支付宝支付" forState:0];
    [payBtn setImage:IMAGE(@"pay") forState:0];
    payBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [payBtn addTarget:self action:@selector(handlePayBtn) forControlEvents:UIControlEventTouchUpInside];
    [payBtn setTitleColor:ColorFrom16(0x333333) forState:0];
    payBtn.titleLabel.font = PFRFONT(14);
    [_bottomView addSubview:payBtn];
    [payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wxBtn.mas_bottom);
        make.left.and.right.equalTo(self.view);
        make.height.equalTo(@44);
    }];
}

//支付宝支付
- (void)handlePayBtn
{
    [GW_Pay alipayPay:_secretKey fromScheme:@"pailexiong" andblock:^(NSDictionary *dict) {
        NSLog(@"%@", dict);
        MESSAGE_TOAST(dict[@"memo"]);
        if ([dict[@"resultStatus"] isEqualToString:@"9000"]) {
            //支付成功,这里放你们想要的操作
            MineRechargeCompleteViewController *VC = [MineRechargeCompleteViewController new];
            [self.navigationController pushViewController:VC animated:YES];
        }
        
    }];
}

//微信支付
- (void)handleWxBtn
{
    [[ShareView sharedInstance] payWithWxDict:_param andCallback:^{
        NSLog(@"微信充值成功");
        MineRechargeCompleteViewController *VC = [MineRechargeCompleteViewController new];
        [self.navigationController pushViewController:VC animated:YES];
    }];
}


- (void)handleButtonAction:(UIButton *)sender
{
    sender.selected = YES;
    _aBtn.selected = NO;
    [sender setTitleColor:ColorFrom16(0xF5CB37) forState:0];
    [_aBtn setTitleColor:ColorFrom16(0x202020) forState:0];
}

- (void)handleAbtnAction:(UIButton *)sender
{
    sender.selected = YES;
    _button.selected = NO;
    [sender setTitleColor:ColorFrom16(0xF5CB37) forState:0];
    [_button setTitleColor:ColorFrom16(0x202020) forState:0];
}


//选择其他充值金额
- (void)handleRechargeBtn
{
    MineAnotherRechargeViewController *VC = [MineAnotherRechargeViewController new];
    [self.navigationController pushViewController:VC animated:YES];
}

//充值按钮的响应方法
- (void)gotoRecharge
{
    
    if(_button.selected == YES)
    {
        _num = _button.titleLabel.text;
    }
    else if(_aBtn.selected == YES)
    {
        _num = _aBtn.titleLabel.text;
    }
    else
    {
        MESSAGE_TOAST(@"请选择要充值的金额");
        return;
    }
    
    [self starNetwork];

    [UIView animateWithDuration:0.3 animations:^{
        _bottomView.hidden = NO;
    }];
    

}

//关闭按钮响应方法
- (void)handleClose
{
    [UIView animateWithDuration:0.3 animations:^{
        _bottomView.hidden = YES;
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
